define(function (require, exports, module) {

	var form = $("#ys-form");

	function init() {

		$("#checkall").click(function() {
			var t = this;
			$("[id^='box_']").each(function() {
				this.checked = t.checked
			});
			checkedNum();
		});

		$("[id^='box_']").click(function() {
			checkedNum();
		});
		
		$("#btn").click(function() {

			if($("#btnsub").css("display")=="block"){
				$("#btnsub").slideUp();
			}else{
				if(validate()){

					$("#btnsub").slideDown();
					setTimeout(function(){
						$("#btnsub").slideUp();
					},2000);
				}
			}
		});

		$
	}
	
	function checkedNum() {
		var num = 0;
		$("[id^='box_']").each(function() {
			if (this.checked) {
				num++;
			}
		});
		$("#itemcount").text(num);
	}

	function showsuc(sysmsg){
		if(sysmsg){
			$("#syss").text(sysmsg);
			$("#syss").slideDown();

			setTimeout(function(){
				$("#syss").slideUp();
			},2000);
		}
	}

	function showerror(sysmsg,id){
		if(sysmsg){
			$("#syse").text(sysmsg);
			$("#syse").slideDown();

			setTimeout(function(){
				$("#syse").slideUp();
			},2000);
		}
		if(id!=undefined){
			$("#"+id).addClass("form-item-error");
			$("#"+id).focus();
		}
	}

	function validate() {
		var num = 0;
		$("[id^='box_']").each(function() {
			if (this.checked) {
				num++;
			}
		});

		if(num<=0){
			showerror("您还没有选中任何数据");
			return false;
		}

		return true;
	}
	var busy=false;
	//提交表单
	function formSubmit(form) {
		if(busy){
			return false;
		}
		busy=true;
		$btnRegister = $("#btnsub");

		var param = $(form).serialize();
		var ajaxurl = '/index.php?';
		$.ajax({
			type: 'post',
			url: ajaxurl + location.search.substring(1),
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			data: param,
			cache:false,
			beforeSend: function () {
				$btnRegister.text('正在生成..');
			},
			error: function () {
				showDialog('网络繁忙，请稍后再试');
			},
			success: function (response) {
				if (response) {
					try{
						var obj = eval(response);

						if (obj.success) {
							successRedirectURL(obj.success);
						}

						if (obj.info) {
							busy=false;
							showerror(obj.info);
							$btnRegister.text('生成集港计划？点击确认');
						}
					}
					catch(e){busy=false;showerror('发生错误，请重试');$btnRegister.text('生成集港计划？点击确认');}

				}
				busy=false;
				$btnRegister.text('生成集港计划？点击确认');
			}
		});
	}

	$("#btnsub").click(function(){
		if(validate()){
			formSubmit("#ys-form");
		}
	});

	function initPlaceholer() {
		$('input[placeholder]').placeholder({
			isValue: true,
			topDiff: 1
		});
	}

	function showDialog(content) {
		$('body').dialog({
			title: '提示',
			fixed: true,
			width: 380,
			//height: 150,
			maskClose: true,
			type: 'html',
			source: '<div class="registerDialog">\
				<div class="ico"></div>\
				<div class="con">\
				' +
				content +
				'\
				</div></div>'
		})
	}
	exports.init = init;
})
function successRedirectURL(url){
	var isIE = !-[1,];
	if(isIE){
		var link = document.createElement("a");
		link.href = url;
		link.style.display = 'none';
		document.body.appendChild(link);
		link.target = "_top";
		link.click();
	}else{
		window.top.location = url;
	}
}